<template>
	<view>
	    <view>
	        <view class='header'>
	            <view class="userinfo-avatar">
	            	<open-data type="userAvatarUrl" lang="zh_CN" />
	            </view>
	        </view>
	
	        <view class='content'>
	            <view>申请获取以下权限</view>
	            <text>获得你的公开信息(昵称，头像等)</text>
	        </view>
	
	        <button class='bottom' type='primary' @click="login" v-if="ischeck" >
	            授权登录
	        </button>
			<button class='bottom' type='primary' @click="login_zheshow" v-else>
			    授权登录
			</button>
			<btnlogin :zheshow='zheshow' v-if="zheshow" />
	    </view>
	</view>
</template>
 
<script>
	import btnlogin from '@/components/butlogin/butlogin';
	const context = require("../../context/ggyzContext.js");
	export default {
		data() {
			return {
				code:'',
				ischeck:true,
				zheshow:false,
				nickname:"",
				avatar:"",
			}
		},
		components:{
			btnlogin
		},
		onShow() {
			var that=this;
			wx.login({
			  success(res) {
				console.log("code:",res.code);
			    if (res.code) {
					that.code=res.code;
			    } else {
			      console.log('登录失败！' + res.errMsg)
			    }
			  }
			})
			
			var {SDKVersion} = wx.getSystemInfoSync()
			// 判断是否支持getUserProfile()获取头像昵称
			var compareRes = this.compareVersion(SDKVersion, "2.21.2");
			// 不支持
			if (compareRes !== -1) {
			  console.log("不支持getUserProfile()获取头像")
			  this.ischeck = false;
			  return
			}
			// 支持
			console.log("支持getUserProfile()获取头像")
		},
		methods: {
			login_zheshow(){
				this.zheshow = true;
			},
			loset(Logon_Credentials){
				console.log(Logon_Credentials,'登录信息');
				this.avatar=Logon_Credentials.active;
				this.nickname=Logon_Credentials.nickname;
				this.login();
			},
			close(){
				this.zheshow=false;
			},
			/**
			 * 版本比较
			 * v1 >= v2 返回 0或1 否则 -1
			 * @param {String} v1
			 * @param {String} v2 
			 */
			compareVersion (v1, v2) {
			  v1 = v1.split('.')
			  v2 = v2.split('.')
			  const len = Math.max(v1.length, v2.length)
			 
			  while (v1.length < len) {
			    v1.push('0')
			  }
			  while (v2.length < len) {
			    v2.push('0')
			  }
			 
			  for (let i = 0; i < len; i++) {
			    const num1 = parseInt(v1[i])
			    const num2 = parseInt(v2[i])
			 
			    if (num1 > num2) {
			      return 1
			    } else if (num1 < num2) {
			      return -1
			    }
			  }
			 
			  return 0
			},
			login(){
				var that=this;
				wx.getUserProfile({
				      desc: '用于完善会员资料',
				      success: e => {
				        console.log("授权信息：",e);
				        //发起网络请求
				        context.request({
				          url: context.constant.url.login,
						  method:'POST',
				          data: {
				            encryptedData: e.encryptedData,
				            iv: e.iv,
				            code: that.code,
							nickname:that.nickname,
							avatar:that.avatar
				          },
				          success(res) {
							console.log(res.data);
							if(res.data.code==1){
								uni.setStorageSync('userInfo',res.data.data);
								uni.setStorageSync('token',res.data.data.token);
								uni.setStorageSync('loginFlag',{expireTime:res.data.data.expiretime})
								uni.switchTab({
									url:'/pages/my/my'
								})
							}else{
								setTimeout( () => {
									uni.showToast({
									title: res.data.msg,
									icon: "none",
								});
								setTimeout( () =>{
									wx.hideToast();
								},2000)
								},0);
							}
				          }
				        })
					  }
				})
			}
		}
	}
</script>
 
<style>
	page{
		background: #FFFFFF;
	}
.header {
    margin: 90rpx 90rpx 90rpx 50rpx;
    border-bottom: 1px solid #ccc;
    text-align: center;
    width: 650rpx;
    height: 300rpx;
    line-height: 450rpx;
	display: flex;
	justify-content: center;
	align-items: center;
}
 
.header .userinfo-avatar {
    width: 200rpx;
    height: 200rpx;
	margin-bottom: 80rpx;
}
 
.content {
    margin-left: 50rpx;
    margin-bottom: 90rpx;
}
 
.content text {
    display: block;
    color: #9d9d9d;
    margin-top: 40rpx;
}
 
.bottom {
    border-radius: 80rpx;
    margin: 70rpx 50rpx;
    font-size: 35rpx;
}
</style>